package com.xiaoyu.lingdian.tool.photo;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

/**
 * 图片生成
 *
 */
@SuppressWarnings("restriction")
public class CreatePicUtil {

	protected final Logger logger = LoggerFactory.getLogger("BASE_LOGGER");

	/**
	 * 生成签名图片
	 */
	public byte[] markImageByIcon(String content) {
		byte[] bytes = null;
		try {
			Font f = new Font("微软雅黑", Font.PLAIN, 12);// 字体，样式,和字号
			FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(f);
			//计算字符串的长度
			// 单个字符宽度fm.charWidth('A')
			int allWidth = fm.stringWidth(String.valueOf(content));
			int allHeight = fm.getHeight();
			
	            BufferedImage buffImg = new BufferedImage(allWidth, allHeight, BufferedImage.TYPE_INT_RGB);  
	            Graphics2D gd = buffImg.createGraphics();  
	            //设置透明  start  
	            buffImg = gd.getDeviceConfiguration().createCompatibleImage(allWidth, allHeight, Transparency.TRANSLUCENT);  
	            gd=buffImg.createGraphics();  
	            //设置透明  end  
	            gd.setFont(f); //设置字体  
	            gd.setColor(Color.ORANGE); //设置颜色  
	            //gd.drawRect(0, 0, allWidth + 2, allHeight + 2); //画边框  
	            gd.drawString(content, 0,allHeight); //输出文字（中文横向居中）  
			
			ByteOutputStream out = new ByteOutputStream();
			ImageIO.write(buffImg, "PNG", out);
            bytes = out.getBytes();
            try {
            	if (out != null) {
            		out.close();
     			}
			} catch (Exception e) {
			}           
            logger.info("文件图片成功");
		} catch (Exception e) {
			logger.info("生成图片失败");
		}
		
		return bytes;
	}

}
